using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class Controls_EditFields : System.Web.UI.UserControl
{
    SqlConnection sConn;
    protected void Page_Load(object sender, EventArgs e)
    {
          GridViewFieldbind();
    }
    public void DbConnection()
    {
        sConn = new SqlConnection(ConfigurationManager.ConnectionStrings["PortalConnectionString"].ConnectionString);
        sConn.Open();
    }
    public void GridViewFieldbind()
    {

        DbConnection();
        string selectFieldQ = "SELECT [FID],[FTEXT],[F_DEPENDENT_ON],[BUS_TYPE] FROM [AvivaTPortal].[dbo].[TC_FEILDS]";
        SqlDataAdapter AdapField = new SqlDataAdapter(selectFieldQ, sConn);
        DataTable dtField = new DataTable();
        AdapField.Fill(dtField);
        GridViewField.DataSource = dtField;
        GridViewField.DataBind();
        sConn.Close();
    }
    protected void GridViewField_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridViewField.EditIndex = -1;
        GridViewFieldbind();
    }
    protected void GridViewField_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
  
    protected void GridViewField_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        DbConnection();
        GridViewRow row = (GridViewRow)GridViewField.Rows[e.RowIndex];
        Label lbldeleteFID = (Label)row.FindControl("lblFieldID");
        string deleteItem = "DELETE FROM [AvivaTPortal].[dbo].[TC_FEILDS] where [FID]=" + lbldeleteFID.Text + "";
        SqlCommand cmd = new SqlCommand(deleteItem, sConn);
        cmd.ExecuteNonQuery();
        sConn.Close();
        GridViewFieldbind();
    }
    protected void GridViewField_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        //lblMessage.Text = "Record Deleted";
    }
    protected void GridViewField_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridViewField.EditIndex = e.NewEditIndex;
        GridViewFieldbind();
    }

    protected void GridViewField_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        //lblMessage.Text = "Record Updated";
    }
    protected void GridViewField_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        DbConnection();
        GridViewRow row = (GridViewRow)GridViewField.Rows[e.RowIndex];

        Label lbl = (Label)row.FindControl("lblFieldID");
        TextBox texFieldName = (TextBox)row.FindControl("txtFieldText");
        TextBox texFieldDepend = (TextBox)row.FindControl("lblFieldDepend");
        TextBox texBusType = (TextBox)row.FindControl("txtBusType");
        

        GridViewField.EditIndex = -1;
        if (texBusType.Text == "")
        {
            //set msg
        }
        if (texFieldDepend.Text == "")
        {
            texFieldDepend.Text = "Null";
        }
        string updateMenuQ = "UPDATE [AvivaTPortal].[dbo].[TC_FEILDS]" + " " +
            "SET [FTEXT] = '" + texFieldName.Text + "'" + " , " +
            "[F_DEPENDENT_ON] = " + texFieldDepend.Text + "" + " ," +
            "[BUS_TYPE] = " + texBusType.Text + "" + ", " +
            
            "WHERE [FID]=" + (lbl.Text) + "";

        SqlCommand cmd = new SqlCommand(updateMenuQ, sConn);
        cmd.ExecuteNonQuery();
        sConn.Close();

        GridViewFieldbind();
    }
    public void AddNewField(object sender, EventArgs e)
    {
        //this.ClientScript.RegisterStartupScript(this.Page.GetType(), "Alert", "<script language=\"javascript\" type=\"text/javascript\" > alert(\"Error: Hello Edit Me\");</script>", false);
        DbConnection();
        //Label lbl = (Label)row.FindControl("lblMenuID");
        TextBox texFieldName = (TextBox)GridViewField.FooterRow.FindControl("txtFieldText");
        TextBox texFieldDepend = (TextBox)GridViewField.FooterRow.FindControl("lblFieldDepend");
        TextBox texBusType = (TextBox)GridViewField.FooterRow.FindControl("txtBusType");
       
        if (texBusType.Text == "")
        {
           //msg
        }
        if (texFieldDepend.Text == "")
        {
            texFieldDepend.Text = "Null";
        }
        string InsertMenuQ = "INSERT INTO [AvivaTPortal].[dbo].[TC_FEILDS]([FTEXT],[F_DEPENDENT_ON],[BUS_TYPE])" +
                             "VALUES('" + texFieldName.Text.ToString().Trim() + "'," + texFieldDepend.Text.ToString().Trim() + ",'" + texBusType.Text.ToString().Trim() + "')";

        SqlCommand cmdInsert = new SqlCommand(InsertMenuQ, sConn);
        cmdInsert.ExecuteNonQuery();
        sConn.Close();

        GridViewFieldbind();
    }
   
   

}
